﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Weibo.Data;
using System.Data.Common;
using System.Data;
using Weibo.Entity;
using Weibo.Common;
using System.Data.SqlClient;

namespace Weibo.Data.SqlServer
{
    public partial class DataProvider : IDataProvider
    {
        public int CreateSchool(School school)
        {
            DbParameter[] parms =
            {
                DbHelper.MakeInParam("@Name",(DbType)SqlDbType.NVarChar,50,school.Name),
                DbHelper.MakeInParam("@Location",(DbType)SqlDbType.Int,4,school.Location),
                DbHelper.MakeInParam("@Type",(DbType)SqlDbType.Int,4,school.Type),
                DbHelper.MakeInParam("@FrontLetter",(DbType)SqlDbType.NChar,0,school.FrontLetter)
            };
            return TypeConverter.ObjectToInt(DbHelper.ExecuteScalar(CommandType.StoredProcedure, "CreateSchool", parms), -1);
        }

        public string SaveClassPicPath(ClassSavePicPath picPathModel)
        {
            SqlConnection conn = new SqlConnection(DbHelper.ConnectionString);
            conn.Open();
            int classPhotoID = -1;
            using (SqlTransaction trans = conn.BeginTransaction())
            {
                 
                try
                {
                    DbParameter[] parms =
                    {
                        DbHelper .MakeInParam("@uid",(DbType)SqlDbType.BigInt,8, picPathModel.uid),
                        DbHelper .MakeInParam("@gid",(DbType)SqlDbType.BigInt,8, picPathModel.gid),
                        DbHelper .MakeInParam("@classPhotoID",(DbType)SqlDbType.Int,4, picPathModel.classPhotoID),
                        DbHelper .MakeInParam("@PhotoName",(DbType)SqlDbType.NVarChar,100,picPathModel.PhotoName) ,
                        DbHelper .MakeInParam("@CreateIP",(DbType)SqlDbType.NVarChar,100,picPathModel.CreateIP)  
                    };
                    classPhotoID = TypeConverter.ObjectToInt(DbHelper.ExecuteScalar(trans, CommandType.StoredProcedure, "SaveClassPhoto", parms), -1);

                    if (classPhotoID < 1)
                    {
                        trans.Rollback();
                         
                        return "创建班级相册出现异常";
                    }
                    else
                    {
                        if (picPathModel.ClassSavePhotoList != null && picPathModel.ClassSavePhotoList.Count>0)
                        {
                            foreach (var picPath in picPathModel.ClassSavePhotoList)
                            {
                                DbParameter[] parmsPicPath =
                            {
               
                                DbHelper .MakeInParam("@ClassPhotoID",(DbType)SqlDbType.Int,4,classPhotoID),
                                DbHelper .MakeInParam("@PicPath",(DbType)SqlDbType.NVarChar,100,picPath)  
                            };
                                DbHelper.ExecuteScalar(trans, CommandType.StoredProcedure, "SaveClassPicPath", parmsPicPath);
                            }
                        }
                       
                    }
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    return "创建班级相册内容出现异常";
                }

                trans.Commit();

            }


            return "OK";
        }

        public DataTable GetShoolsByLetLocType(int Location, string letter, int type)
        {
            DbParameter[] parms =
            {
                DbHelper.MakeInParam("@Letter",(DbType)SqlDbType.NChar,50,letter),
                DbHelper.MakeInParam("@Location",(DbType)SqlDbType.Int,4,Location),
                DbHelper.MakeInParam("@Type",(DbType)SqlDbType.Int,4,type),
            };
            return DbHelper.ExecuteDataset(CommandType.StoredProcedure, "GetShoolsByLetLocType", parms).Tables[0];
        }
 

        public DataTable GetClassPhotoList(long uid, long gid)
        {
           
            DbParameter[] parms =
            {
                DbHelper.MakeInParam("@uid",(DbType)SqlDbType.BigInt,8,uid),
                DbHelper.MakeInParam("@gid",(DbType)SqlDbType.BigInt,8,gid) 
            };
            return DbHelper.ExecuteDataset(CommandType.StoredProcedure, "GetClassPhotoList", parms).Tables[0];
        }

        public DataTable GetClassPhotoDetailList(long classPhotoID, int page, int pageSize, ref int pageCount)
        {
            return Pager(page, pageSize, "View_GetClassPhotoDetailList", " classPhotoID=" + classPhotoID + " and ImagePath<>'' order by ID desc ", ref pageCount);
        }
        public DataTable GetClassPhotoDetailListNoPage(long classPhotoID)
        {
            DbParameter[] parms =
            {
                DbHelper.MakeInParam("@classPhotoID",(DbType)SqlDbType.BigInt,8,classPhotoID) 
            };

            return DbHelper.ExecuteDataset(CommandType.StoredProcedure, "GetClassPhotoDetailListNoPage", parms).Tables[0];
        }
        public DataTable GetShoolsCollegeByLetLocType(int uid)
        {
            return GetTableByQuery("TCollege", string.Format("uid={0}", uid), " * ", "id", "desc");
        }
        public DataTable GetShoolsClassByLetLocType(int schoolID, int collegeID)
        {
            return GetTableByQuery("TSchoolClass", string.Format("uid={0} and flag=1", collegeID), " * ", "id", "desc");
        }
        public IDataReader GetSchoolByID(int id)
        {
            return GetRowByID("TSchool", id, DbFields.SCHOOL);
        }

        public DataTable GetSchoolsByLikeName(string name)
        {
            return GetTop10TableByQuery("TSchool", string.Format("Name like '%{0}%' or Name like '{0}%' or Name like '%{0}' or Name like '{0}'", name), DbFields.SCHOOL, "CreateTime", "desc");
        }

        public DataTable SearchSchoolMiniBlog(int pageIndex, int pageSize, ref int rowCount, string key, int isOri, int location, DateTime? startTime, DateTime? endTime, int isMyself, int isMyFollow, long curUid, string someone, int isHavePic, int isHaveLink, int isHaveVideo, int isHaveMusic, int isHaveVote, string sort, long GroupID, int IsFriendShip)
        {
            DbParameter[] parms =
            {
                DbHelper.MakeInParam("@PageIndex",(DbType)SqlDbType.Int,4,pageIndex),
                DbHelper.MakeInParam("@PageSize",(DbType)SqlDbType.Int,4,pageSize),
                DbHelper.MakeOutParam("@rowcount",(DbType)SqlDbType.Int,4),
                DbHelper.MakeInParam("@Key",(DbType)SqlDbType.NVarChar,0,key),
                DbHelper.MakeInParam("@IsOri",(DbType)SqlDbType.Int,4,isOri),
                DbHelper.MakeInParam("@Location",(DbType)SqlDbType.Int,4,location),
                DbHelper.MakeInParam("@StartTime",(DbType)SqlDbType.DateTime,4,startTime),
                DbHelper.MakeInParam("@EndTime",(DbType)SqlDbType.DateTime,4,endTime),
                DbHelper.MakeInParam("@IsMyself",(DbType)SqlDbType.Int,4,isMyself),
                DbHelper.MakeInParam("@IsMyFollow",(DbType)SqlDbType.Int,4,isMyFollow),
                DbHelper.MakeInParam("@CurUID",(DbType)SqlDbType.Int,8,curUid),
                DbHelper.MakeInParam("@SomeOne",(DbType)SqlDbType.NVarChar,50,someone),
                DbHelper.MakeInParam("@IsHavePic",(DbType)SqlDbType.Int,4,isHavePic),
                DbHelper.MakeInParam("@IsHaveLink",(DbType)SqlDbType.Int,4,isHaveLink),
                DbHelper.MakeInParam("@IsHaveVideo",(DbType)SqlDbType.Int,4,isHaveVideo),
                DbHelper.MakeInParam("@IsHaveMusic",(DbType)SqlDbType.Int,4,isHaveMusic),
                DbHelper.MakeInParam("@IsHaveVote",(DbType)SqlDbType.Int,4,isHaveVote),
                DbHelper.MakeInParam("@Sort",(DbType)SqlDbType.VarChar,100,sort),
                                DbHelper.MakeInParam("@GroupID",(DbType)SqlDbType.BigInt,8,GroupID),
                DbHelper.MakeInParam("@IsFriendShip",(DbType)SqlDbType.Int,4,IsFriendShip)
            };

            DataTable dt = DbHelper.ExecuteDataset(CommandType.StoredProcedure, "SearchSchoolMiniBlog", parms).Tables[0];
            rowCount = TypeConverter.ObjectToInt(parms[2].Value);
            return dt;
        }

        public DataTable GetTop500SchoolMiniBlogsByQuery(string @where, string sortField, string sortType)
        {
            return GetTop500TableByQuery("TMiniBlog", where, DbFields.MINIBLOG, sortField, sortType);
        }

        public DataTable SearchSchoolMiniBlogForMbIndexAdvanced(int pageIndex, int pageSize, ref int rowCount, string key, int isOri, int isRet, DateTime? startTime, DateTime? endTime, int isMyself, int isMyFollow, long curUid, int isHavePic, int isHaveLink, int isHaveVideo, int isHaveMusic, int isHaveVote, string sort, long GroupID, int IsFriendShip)
        {
            DbParameter[] parms =
            {
                DbHelper.MakeInParam("@PageIndex",(DbType)SqlDbType.Int,4,pageIndex),
                DbHelper.MakeInParam("@PageSize",(DbType)SqlDbType.Int,4,pageSize),
                DbHelper.MakeOutParam("@rowcount",(DbType)SqlDbType.Int,4),
                DbHelper.MakeInParam("@Key",(DbType)SqlDbType.NVarChar,0,key),
                DbHelper.MakeInParam("@IsOri",(DbType)SqlDbType.Int,4,isOri),
                DbHelper.MakeInParam("@IsRet",(DbType)SqlDbType.Int,4,isRet),
                DbHelper.MakeInParam("@StartTime",(DbType)SqlDbType.DateTime,4,startTime),
                DbHelper.MakeInParam("@EndTime",(DbType)SqlDbType.DateTime,4,endTime),
                DbHelper.MakeInParam("@IsMyself",(DbType)SqlDbType.Int,4,isMyself),
                DbHelper.MakeInParam("@IsMyFollow",(DbType)SqlDbType.Int,4,isMyFollow),
                DbHelper.MakeInParam("@CurUID",(DbType)SqlDbType.Int,8,curUid),
                DbHelper.MakeInParam("@IsHavePic",(DbType)SqlDbType.Int,4,isHavePic),
                DbHelper.MakeInParam("@IsHaveLink",(DbType)SqlDbType.Int,4,isHaveLink),
                DbHelper.MakeInParam("@IsHaveVideo",(DbType)SqlDbType.Int,4,isHaveVideo),
                DbHelper.MakeInParam("@IsHaveMusic",(DbType)SqlDbType.Int,4,isHaveMusic),
                DbHelper.MakeInParam("@IsHaveVote",(DbType)SqlDbType.Int,4,isHaveVote),
                DbHelper.MakeInParam("@Sort",(DbType)SqlDbType.VarChar,100,sort),
                                DbHelper.MakeInParam("@GroupID",(DbType)SqlDbType.BigInt,8,GroupID),
                DbHelper.MakeInParam("@IsFriendShip",(DbType)SqlDbType.Int,4,IsFriendShip)
            };

            DataTable dt = DbHelper.ExecuteDataset(CommandType.StoredProcedure, "SearchMiniBlogForMbIndexAdvanced", parms).Tables[0];
            rowCount = TypeConverter.ObjectToInt(parms[2].Value);
            return dt;
        }

        public IDataReader GetSinglePhotoDetail(long photoID)
        {
            DbParameter[] parms =
            {
                DbHelper .MakeInParam("@photoID",(DbType)SqlDbType.Int,8,photoID)
            };
            return DbHelper.ExecuteReader(CommandType.StoredProcedure, "GetSinglePhotoDetail", parms);
        }

        public string DelPhoto(long photoID)
        {
            DbParameter[] parms =
                    {
                        DbHelper .MakeInParam("@photoID",(DbType)SqlDbType.BigInt,8, photoID)  
                    };
            return DbHelper.ExecuteScalar(CommandType.StoredProcedure, "DelPhoto", parms).ToString();
        }

        public string DelPhotoFolder(long classPhotoID)
        {
            DbParameter[] parms =
                    {
                        DbHelper .MakeInParam("@classPhotoID",(DbType)SqlDbType.BigInt,8, classPhotoID)  
                    };
            return DbHelper.ExecuteScalar(CommandType.StoredProcedure, "DelPhotoFolder", parms).ToString();
        }
    }
}
